<template>
	<view class="container">
		<!-- 顶部卡片 -->
		<view class="header-card">
			<view class="month-selector">
				<text class="current-month">{{currentDate}}</text>
			</view>
			<view class="overview-stats">
				<view class="stat-item">
					<text class="stat-label">本月支出</text>
					<text class="stat-amount expense">-{{toTalList[1].num}}</text>
				</view>
				<view class="divider"></view>
				<view class="stat-item">
					<text class="stat-label">本月收入</text>
					<text class="stat-amount income">{{toTalList[0].num}}</text>
				</view>
			</view>
		</view>

		<!-- 账单列表 -->
		<scroll-view class="bill-list" scroll-y>
			<view class="bill-group" v-for="(dayItem,dayIndex) in BillList" :key="dayIndex">
				<view class="date-header">
					<view class="date-info">
						<text class="date-text">{{dayItem._id}}</text>
						<text class="week-text">周五</text>
					</view>
					<view class="daily-summary">
						<text class="summary-text">支出 <text class="expense-amount">{{dayItem.ExpenseTotal}}</text></text>
						<text class="summary-text">收入 <text class="income-amount">{{dayItem.IncomeTotal}}</text></text>
					</view>
				</view>

				<view class="bill-items">
					<view class="bill-item" v-for="(item,index) in dayItem.Arr" :key="index" 
						@click="showBillDetail(item)">
						<view class="item-left">
							<view class="icon-wrapper" :class="item.topOption == 0 ? 'income-icon' : 'expense-icon'">
								<image class="category-icon" :src="item.OptionTypeicon"></image>
							</view>
							<view class="item-info">
								<text class="category-name">{{item.OptionType}}</text>
								<text class="item-note" v-if="item.OptionNote">{{item.OptionNote}}</text>
							</view>
						</view>
						<text class="item-amount" :class="item.topOption == 0 ? 'income' : 'expense'">
							{{item.topOption == 0 ? '+' : '-'}}{{item.intCount}}
						</text>
					</view>
				</view>
			</view>
		</scroll-view>

		<!-- 浮动按钮 -->
		<view class="float-buttons">
			<view class="float-btn chart-btn" @click="SeePicture">
				<image src="../../static/fsux.png" mode="aspectFit"></image>
			</view>
			<view class="float-btn add-btn" @click="AddBill">
				<image src="../../static/addfilled.png" mode="aspectFit"></image>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {

			return {
				toTalList: [{
						text: '收入',
						num: 0,
						baccolor: '#afffff',
						Color : '#10b0f0'
					},
					{
						text: '支出',
						num: 0,
						baccolor: '#ffcfcf',
						Color:'#ff1f1f'
					}
				],
				BillList: {},
				currentDate: ''
			}
		},
		onReady() {
			uni.showLoading({
				title:"加载中..."
			})
			this.upBillList()
			this.getCurrentDate()
		},
		onShow() {
			this.upBillList()
		},
		methods: {
			SeePicture(){
				uni.navigateTo({
					url:'/pages/Picture/Picture'
				})
			},
			AddBill() {
				uni.navigateTo({
					url: '/pages/Money/Money'
				})
			},
			upBillList() {
				uniCloud.callFunction({
					name: 'searchBill',
					data: ''
				}).then(x => {
					uni.hideLoading()
					this.BillList = x.result.res.data
					this.toTalList[0].num = x.result.AllTotal.AllIncomeTotal
					this.toTalList[1].num = x.result.AllTotal.AllExpenseTotal
				}).catch(err => {
					console.log(err)
				})
			},
			showBillDetail(item) {
				uni.showModal({
					title: item.OptionType,
					content: `金额: ${item.topOption==0 ? '+' : '-'}${item.intCount}\n时间: ${item.date}\n备注: ${item.OptionNote || '无'}`,
					showCancel: false
				})
			},
			getCurrentDate() {
				const date = new Date()
				const year = date.getFullYear()
				const month = date.getMonth() + 1
				this.currentDate = `${year}年${month}月`
			}
		}
	}
</script>

<style>
.container {
	min-height: 100vh;
	background-color: #f7f7f7;
	padding-bottom: 120rpx;
}

.header-card {
	background: linear-gradient(135deg, #7F7FD5, #86A8E7, #91EAE4);
	padding: 40rpx 30rpx;
	color: #fff;
}

.month-selector {
	margin-bottom: 30rpx;
}

.current-month {
	font-size: 32rpx;
	font-weight: 500;
}

.overview-stats {
	display: flex;
	justify-content: space-around;
	align-items: center;
	background: rgba(255, 255, 255, 0.1);
	border-radius: 20rpx;
	padding: 30rpx;
}

.stat-item {
	display: flex;
	flex-direction: column;
	align-items: center;
}

.divider {
	width: 2rpx;
	height: 60rpx;
	background: rgba(255, 255, 255, 0.2);
}

.stat-label {
	font-size: 28rpx;
	margin-bottom: 10rpx;
	opacity: 0.9;
}

.stat-amount {
	font-size: 44rpx;
	font-weight: 600;
}

.bill-list {
	padding: 30rpx 20rpx;
}

.bill-group {
	background: #fff;
	border-radius: 20rpx;
	margin-bottom: 20rpx;
	overflow: hidden;
	box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.02);
}

.date-header {
	display: flex;
	justify-content: space-between;
	align-items: center;
	padding: 20rpx 30rpx;
	background: #f9f9f9;
}

.date-info {
	display: flex;
	align-items: center;
}

.date-text {
	font-size: 28rpx;
	color: #333;
	margin-right: 16rpx;
}

.week-text {
	font-size: 24rpx;
	color: #999;
}

.daily-summary {
	font-size: 24rpx;
	color: #666;
}

.summary-text {
	margin-left: 20rpx;
}

.bill-items {
	padding: 0 20rpx;
}

.bill-item {
	display: flex;
	justify-content: space-between;
	align-items: center;
	padding: 30rpx 20rpx;
	border-bottom: 1rpx solid #f5f5f5;
}

.item-left {
	display: flex;
	align-items: center;
}

.icon-wrapper {
	width: 80rpx;
	height: 80rpx;
	border-radius: 50%;
	display: flex;
	align-items: center;
	justify-content: center;
	margin-right: 20rpx;
}

.income-icon {
	background: rgba(82, 196, 26, 0.1);
}

.expense-icon {
	background: rgba(255, 77, 79, 0.1);
}

.category-icon {
	width: 44rpx;
	height: 44rpx;
}

.item-info {
	display: flex;
	flex-direction: column;
}

.category-name {
	font-size: 30rpx;
	color: #333;
	margin-bottom: 6rpx;
}

.item-note {
	font-size: 24rpx;
	color: #999;
}

.item-amount {
	font-size: 32rpx;
	font-weight: 500;
}

.income {
	color: #52c41a;
}

.expense {
	color: #ff4d4f;
}

.float-buttons {
	position: fixed;
	right: 30rpx;
	bottom: 140rpx;
	display: flex;
	flex-direction: column;
	gap: 20rpx;
}

.float-btn {
	width: 100rpx;
	height: 100rpx;
	background: #fff;
	border-radius: 50%;
	display: flex;
	align-items: center;
	justify-content: center;
	box-shadow: 0 4rpx 16rpx rgba(0,0,0,0.1);
	transition: all 0.3s ease;
}

.float-btn image {
	width: 60rpx;
	height: 60rpx;
	object-fit: contain;
}

.chart-btn {
	background: #ffffff;
}

.add-btn {
	background: #ffffff;
}

.add-btn image {
	width: 60rpx;
	height: 60rpx;
	filter: none;
}

.float-btn:active {
	transform: scale(0.95);
	opacity: 0.8;
}
</style>